/*
# pymoz, Python extension for the Mozilla Toolkit
# Copyright (C) 2012 Malek Hadj-Ali
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef pyException_h_
#define pyException_h_


#include "pymoz.h"

#include "nsIException.h"
#include "nsIScriptError.h"

#pragma GCC visibility push(default)
#include "frameobject.h"
#pragma GCC visibility pop


void
PyTraceback_FromLocation(const char *filename, const char *name, int lineno);


class pyStackFrame : public nsIStackFrame
{
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSISTACKFRAME

    static already_AddRefed<nsIStackFrame>
    Create(PyFrameObject *aFrame, long limit);

    pyStackFrame();

private:
    ~pyStackFrame();

    bool Init(PyFrameObject *aFrame, long limit);

    nsCString mFilename;
    nsCString mName;
    PRInt32 mLineNumber;
    nsCString mSourceLine;
    nsCOMPtr<nsIStackFrame> mCaller;
};


class pyException : public nsIException
{
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSIEXCEPTION

    static already_AddRefed<nsIException>
    Create(PyObject *aExcValue, nsresult aResult = NS_ERROR_FAILURE);

    pyException();

private:
    ~pyException();

    bool Init(PyObject *aExcValue, nsresult aResult);

    nsCString mMessage;
    nsresult mResult;
    nsCString mName;
    nsCString mFilename;
    PRUint32 mLineNumber;
    PRUint32 mColumnNumber;
    nsCString mSourceLine;
    nsCOMPtr<nsIStackFrame> mLocation;
    nsCOMPtr<nsIException> mContext;
    nsCOMPtr<nsIException> mCause;
};


class pyWarning : public nsIScriptError
{
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSICONSOLEMESSAGE
    NS_DECL_NSISCRIPTERROR

    static already_AddRefed<nsIScriptError>
    Create(PyObject *aMessage, PyObject *aCategory,
           PyObject *aFilename, PyObject *aLineno, PyObject *aLine);

    pyWarning();

private:
    ~pyWarning();

    bool Init(PyObject *aMessage, PyObject *aCategory,
              PyObject *aFilename, PyObject *aLineno, PyObject *aLine);

    nsCString mErrorMessage;
    nsCString mSourceName;
    nsCString mSourceLine;
    PRUint32 mLineNumber;
};


#endif // pyException_h_
